import {createRouter,createWebHashHistory} from 'vue-router'

let sysUser = defineUser(pinia)
import Login from '../components/Login.vue'
import Register from "../components/Register.vue";
import ShowSchedule from "../components/ShowSchedule.vue";
import {defineUser} from "../store/userStore.js";
import pinia from "../pinia.js";

let  router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            component:Login
        },
        {
            path:"/login",
            component:Login
        },
        {
            path:"/showSchedule",
            component:ShowSchedule
        },
        {
            path:"/register",
            component:Register
        }


    ]
})
//配置路由守卫，在登陆状态中才可以访问主页面
router.beforeEach((to, from, next) => {
    //如果是查看日程
    if (to.path=="/showSchedule"){
        //如果尚未登录
        if (sysUser.username==''){
            alert('您尚未登录，请登录之后查看日程')
            next("/login")
        }else {
            //已经登录，放行
            next()
        }
    }else {
        next()
    }
})
export default router